<template>
	<div class="lm-chatgpt-item">
		<chatgpt-item-header text="正在为您生成结果：定时任务"></chatgpt-item-header>
		<div class="chatgpt-item-content">
			<div class="item-section-title">新增定时任务</div>
			<h-form :model="formItem" ref="form" cols="2" :label-width="58">
				<h-form-item label="触发频率" prop="task_type" required>
					<h-select v-model="formItem.task_type" placeholder="请选择" :disabled="isSubmited">
						<h-option v-for="(item, index) in frequencyList" :key="index"
							:value="item.type" :label="item.label"></h-option>
					</h-select>
				</h-form-item>
				<h-form-item label="触发时间" prop="value1" required>
					<!--单次-->
					<h-date-picker v-if="formItem.task_type == 0" v-model="formItem.value1"  type="datetime" placeholder="请选择" :disabled="isSubmited"></h-date-picker>
					<!-- 每日 -->
					<h-time-picker v-else-if="formItem.task_type == 1" v-model="formItem.value1" type="time" placeholder="请选择"></h-time-picker>
					<!-- 每周 -->
					<h-select v-else-if="formItem.task_type == 2" v-model="formItem.value1" placeholder="请选择" :disabled="isSubmited">
						<h-option v-for="(item, index) in weekList" :key="index"
							:value="item.value" :label="item.label"></h-option>
					</h-select>
					<!-- 每月 -->
					<h-select v-else-if="formItem.task_type == 3" v-model="formItem.value1" placeholder="请选择" :disabled="isSubmited">
						<h-option v-for="(item, index) in dayList" :key="index"
							:value="item.value" :label="item.label"></h-option>
					</h-select>
					<!-- 每年 -->
					<div v-else-if="formItem.task_type == 4" class="year-box">
						<h-select v-model="formItem.value1" placeholder="请选择" :disabled="isSubmited" style="width:80px">
							<h-option v-for="(item, index) in monthList" :key="index"
								:value="item.value" :label="item.label"></h-option>
						</h-select>
						<h-select v-model="formItem.value2" placeholder="请选择" :disabled="isSubmited" style="width:80px">
							<h-option v-for="(item, index) in dayList" :key="index"
								:value="item.value" :label="item.label"></h-option>
						</h-select>
					</div>
				</h-form-item>
				<h-form-item label="事件描述" prop="task_describe" required>
					<h-input type="textarea" v-model="formItem.task_describe" placeholder="请输入" :rows="2" :disabled="isSubmited"></h-input>
				</h-form-item>
			</h-form>
			<div class="bottom-ope">
				<span class="btn text-btn" @click="showTaskList">已有定时任务清单</span>
				<h-button v-if="!isSubmited"  type="info" @click="submit">确定</h-button>
			</div>
		</div>
	</div>
</template>

<script>
import ChatgptItemHeader from './chatgpt-item-header'
import { mapState, mapMutations } from "vuex"
export default {
	name: 'OrdinaryTaskCreate',
	props: {
		item: Object
	},
	components: {
		ChatgptItemHeader
	},
	data() {
		return {
			formItem: {
				task_type: '0',
				value1: '',
				value2: '',
				task_describe: '',
			},
			isSubmited: false, // 是否已提交
			frequencyList: [
				{
					label: '单次',
					type: '0'
				},
				{
					label: '每日',
					type: '1'
				},
				{
					label: '每周',
					type: '2'
				},
				{
					label: '每月',
					type: '3'
				},
				{
					label: '每年',
					type: '4'
				}
			],
			weekList: [],
			monthList: [],
			dayList: []
		}
	},
	computed: {
	},
	watch: {
		'formItem.task_type': {
			handler(val) {
				this.formItem.value1 = ''
			},
			deep: true
		}
	},
	created() {
		this.initWeekList()
		this.initMonthList()
		this.initDayList()
	},
	methods: {
		...mapMutations('lm-chat', ['pushGptMessageList']),
		// 初始化周列表
		initWeekList() {
			for (let i = 0; i < 7; i++) {
				let list = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
				this.weekList.push({
					label: list[i],
					value: i + 1 + ''
				})
			}
		},
		// 初始化月列表
		initMonthList() {
			for (let i = 0; i < 12; i++) {
				this.monthList.push({
					label: i + 1 + '月',
					value: i + 1 + ''
				})
			}
		},
		// 初始化日列表
		initDayList() {
			for (let i = 0; i < 31; i++) {
				this.dayList.push({
					label: i + 1 + '日',
					value: i + 1 + ''
				})
			}
		},
		// 已有定时任务清单
		showTaskList() {
			this.$usePost(this.$url.queryTaskList, {
				 current: 1,
				 size:100,
				 user_id: LOCAL_CONFIG.agent_code
			}).then(res => {
				let msgItem = {
					message_body_type: "scheduledTaskManage",
					message_type: '4',
					tData: res.data.records
				}
				this.pushGptMessageList(msgItem)
			})
		},
		// 提交
		submit() {
			this.$refs.form.validate(valid => {
				if (valid) {
					let params= {
						...this.formItem,
						user_id: LOCAL_CONFIG.agent_code, // 代理人code
					}
					this.$usePost(this.$url.addTaskOne, params).then(res => {
						this.isSubmited = true
						this.$public.message(this, 'success', '提交成功')
					})
				} else {
					this.$public.message(this, 'error', '请填写完整信息')
				}
			})
		}
	}
}
</script>

<style lang="scss" scoped>
.chatgpt-item-content{
	.item-section-title{
		margin-bottom: 16px;
	}
	/deep/ .h-form-item-label{
		&::before{
			display: none;
		}
	}
	.year-box{
		display: flex;
		justify-content: space-between;
	}
	.bottom-ope{
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 10px;
		.text-btn{
			cursor: pointer;
			color: #4686F2;
		}
	}
}
</style>